package com.ynu.znkz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ynu.znkz.commomutils.R;
import com.ynu.znkz.entity.PaperResearchObj;
import com.ynu.znkz.mapper.PaperResearchObjMapper;
import com.ynu.znkz.service.PaperResearchObjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lu
 * @since 2021-05-29
 */
@Service
public class PaperResearchObjServiceImpl extends ServiceImpl<PaperResearchObjMapper, PaperResearchObj> implements PaperResearchObjService {

    //增加一个研究对象
    @Override
    public String savePaperResearchObj(String paperResearchObjName,String parent_id) {
        QueryWrapper<PaperResearchObj> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",paperResearchObjName);
        List<PaperResearchObj> paperResearchObjs = baseMapper.selectList(queryWrapper);
        //在研究对象数据库中没有查到数据
        if(paperResearchObjs.isEmpty()){
            PaperResearchObj paperResearchObj = new PaperResearchObj();
            paperResearchObj.setName(paperResearchObjName);
            paperResearchObj.setParentId(parent_id);
            baseMapper.insert(paperResearchObj);
            return paperResearchObj.getId();
        }else {
            return paperResearchObjs.get(0).getId();
        }
    }

    @Override
    public List<PaperResearchObj> getAllTopLevel() {
        QueryWrapper<PaperResearchObj> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<PaperResearchObj> getSecondLevel() {
        QueryWrapper<PaperResearchObj> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("parent_id", 0);
        return baseMapper.selectList(queryWrapper);
    }
}
